// Define a class that extends OBPopup
isc.defineClass('OBEXAPP_FactoryParameterPopup', isc.OBPopup);

isc.OBEXAPP_FactoryParameterPopup.addProperties({

	width : 500,
	height : 250,
	title : null,
	showMinimizeButton : false,
	showMaximizeButton : false,

	view : null,
	params : null,
	actionHandler : 'com.mes.handler.ShopCalendarHandler',
	//  actionHandler:null,
	orders : null,

	mainformstart : null,
	mainformend : null,
	okButton : null,
	cancelButton : null,

	initWidget : function() {

		// Form that contains the parameters
		this.mainformstart = isc.DynamicForm.create({
			numCols : 1,
			fields : [ {
				name : 'startDate',
				title : '从',
				height : 20,
				width : 250,
				type : '_id_15' //Date reference
			} ]
		});

		this.mainformend = isc.DynamicForm.create({
			numCols : 1,
			fields : [ {
				name : 'endDate',
				title : '到',
				height : 20,
				width : 250,
				type : '_id_15' //Date reference
			} ]
		});

		// OK Button
		this.okButton = isc.OBFormButton.create({
			title : '确定',
			popup : this,
			action : function() {
				var startdate = this.popup.mainformstart.getField('startDate').getValue();
				var enddate = this.popup.mainformend.getField('endDate').getValue();
				var years = startdate.getFullYear();
				var yeare = enddate.getFullYear();
				var months = startdate.getMonth() + 1;
				var monthe = enddate.getMonth() + 1;
				var days = startdate.getDate();
				var daye = enddate.getDate();
				if (startdate == '' || enddate == '' || startdate == null
						|| enddate == null) {
					alert("时间输入有误，请仔细检查。");
					return;
				}

				if (parseInt(years) <= parseInt(yeare)) {
					if (parseInt(months) + 1 <= parseInt(monthe) + 1) {
						if (parseInt(days) <= parseInt(daye)) {
							startdate = years + "-" + months + "-" + days;
							enddate = yeare + "-" + monthe + "-" + daye;

							var callback = function(rpcResponse, data,
									rpcRequest) {
								if(data.flag){
									alert(data.message);
								}
								rpcRequest.clientContext.popup.closeClick();
							};

							OB.RemoteCallManager.call(this.popup.actionHandler,
									{
										ids : this.popup.params,
										action : this.popup.params.action,
										dateParam : startdate + "::" + enddate, //send the parameter to the server too
									}, {}, callback, {
										popup : this.popup
									});
						} else {
							alert("您的时间输入有误，请仔细检查。");
							return;
						}
					} else {
						alert("您的时间输入有误，请仔细检查。");
						return;
					}
				} else {
					alert("您的时间输入有误，请仔细检查。");
					return;
				}

			}
		});

		// Cancel Button
		this.cancelButton = isc.OBFormButton.create({
			title : '取消',
			popup : this,
			action : function() {
				this.popup.closeClick();
			}
		});

		//Add the elements into a layout   
		this.items = [ isc.VLayout.create({
			defaultLayoutAlign : "center",
			align : "center",
			width : "100%",
			layoutMargin : 15,
			membersMargin : 10,
			members : [ isc.HLayout.create({
				defaultLayoutAlign : "center",
				align : "center",
				layoutMargin : 15,
				membersMargin : 10,
				members : [ this.mainformstart, this.mainformend ]
			}), isc.HLayout.create({
				defaultLayoutAlign : "center",
				align : "center",
				membersMargin : 10,
				members : [ this.okButton, this.cancelButton ]
			}) ]
		}) ];

		this.Super('initWidget', arguments);
	}

});
